package fy.uap.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import org.apache.commons.logging.LogFactory;

import fy.config.FyConfig;
import fy.util.DBUtil;

public class ERPMergerCancelDao {

	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(ERPMergerCancelDao.class);

	private String number;
	private List<String> serialList;
	private String warn;

	public String getWarn() {
		return warn;
	}

	public void setSerialList(List<String> serialList) {
		this.serialList = serialList;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public boolean save() {
		boolean success = false;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.connect(FyConfig.getERP_DB_CONFIG());

			conn.setAutoCommit(false);

			pst = conn
					.prepareStatement("delete from apps.FYG_AP_VENDORS_unite u where u.oa_number = ? and u.oa_number_sequence = ? and not exists(  select * from APPS.PO_VENDORs a,     APPS.PO_VENDOR_SITES_ALL b,     apps.AP_DUPLICATE_VENDORS_ALL c where c.duplicate_vendor_id=b.vendor_id   and c.duplicate_vendor_site_id=b.vendor_site_id  and b.vendor_id=a.vendor_id and  a.segment1 = u.erp_bm and u.from_site = b.vendor_site_code and c.org_id = decode(u.syb,'福耀玻璃工业集团股份有限公司',101  ,'福清万达玻璃',102  ,'福清包边',104  ,'福清巴士玻璃',103  ,'万盛配件玻璃',243  ,'上海巴士玻璃',329  ,'上海轿车玻璃',323  ,'北京汽车玻璃',283  ,'广州汽车玻璃',182  ,'湖北汽车玻璃',443  ,'福清机械制造',403  ,'郑州汽车玻璃',483  ,'重庆汽车玻璃',263  ,'长春巴士玻璃',305  ,'长春轿车玻璃',303  ,'集团总办管理处',363  ,'广州佛山汽车玻璃',405	,'福州福耀模具科技有限公司',643	,'沈阳汽车玻璃',584	,'烟台福耀',583,'武汉福耀',803))");
			for (int i = 0, size = serialList.size(); i < size; i++) {
				pst.setString(1, number);
				pst.setString(2, serialList.get(i));
				pst.addBatch();
			}
			pst.executeBatch();
			conn.commit();

			DBUtil.close(pst);
			StringBuilder sb = new StringBuilder()
					.append("select u.oa_number_sequence from apps.FYG_AP_VENDORS_unite u where u.oa_number = ? and (");
			for (int i = 0, size = serialList.size(); i < size; i++) {
				if (i != 0) {
					sb.append(" or ");
				}
				sb.append(" u.oa_number_sequence = ?");
			}
			sb.append(")");

			pst = conn.prepareStatement(sb.toString());
			sb = null;
			pst.setString(1, number);
			for (int i = 0, size = serialList.size(); i < size; i++) {
				pst.setString(i + 2, serialList.get(i));
			}

			rs = pst.executeQuery();

			while (rs.next()) {
				if (sb == null) {
					sb = new StringBuilder().append("《取消供应商合并表》单据编号：")
							.append(number).append("序号：");
				}
				sb.append(rs.getString(1));
				sb.append("   ");
			}
			if (sb != null) {
				sb.append("取消合并失败\n失败原因：该供应商已被合并，无法取消");
				log.warn(sb.toString());
				warn = sb.toString();
			}
			success = true;
		} catch (Exception e) {
			DBUtil.rollback(conn);
			e.printStackTrace();
			log.error("取消供应商合并审批表（ERP）数据写入ERP数据库异常，单据编号：" + number, e);
		} finally {
			DBUtil.setAutoCommit(conn, true);
			DBUtil.close(pst, conn);
		}
		return success;
	}
}
